You configure CC Mode by setting Lisp variables and calling (and perhaps writing) Lisp functions1, which is usually done by adding code to an Emacs initialization file. This file might be site-start.el or .emacs or init.el or default.el or perhaps some other file. See Init File. For the sake of conciseness, we just call this file “your .emacs” throughout the rest of the manual.
Several of these variables (currently 16), are known collectively as style variables. CC Mode provides a special mechanism, known as styles to make it easier to set these variables as a group, to “inherit” settings from one style into another, and so on. Style variables remain ordinary Lisp variables, whose values can be read and changed independently of the style system. See Style Variables.
There are several ways you can write the code, depending on the precise effect you want—they are described further down on this page. If you are new to CC Mode, we suggest you begin with the simplest method, “Top-level commands or the customization interface”.
If you make conflicting settings in several of these ways, the way that takes precedence is the one that appears latest in this list:
Here is a summary of the different ways of writing your configuration settings:
setq and similar
commands at the top level of your .emacs file. When you load a CC Mode
buffer, it initializes its configuration from these global
values (at least, for those settings you have given values
to), so it makes sense to have these setq
commands run before CC Mode is first
initialized—in particular, before any call to
desktop-read (see Saving
Emacs Sessions). For example, you might set
c-basic-offset thus:
(setq c-basic-offset 4)
You can use the more user friendly Customization interface
instead, but this manual does not cover in detail how that
works. To do this, start by typing M-x customize-group
<RET> c <RET>. See Easy
Customization.
Emacs normally writes the customizations at the end of your
.emacs file. If you
use desktop-read, you should edit your
.emacs to place the
call to desktop-read after the
customizations.
The first initialization of CC Mode puts a snapshot of the
configuration settings into the special style
user. See Built-in
Styles.
For basic use of Emacs, either of these ways of
configuring is adequate. However, the settings are then the
same in all CC Mode buffers and it can be clumsy to
communicate them between programmers. For more flexibility,
you'll want to use one (or both) of CC Mode's more
sophisticated facilities, hooks and styles.
c-basic-offset in C Mode and Java Mode buffers,
you could do it like this:
(defun my-c-mode-hook ()
(setq c-basic-offset 3))
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-java-mode-hook ()
(setq c-basic-offset 6))
(add-hook 'java-mode-hook 'my-java-mode-hook)
See CC Hooks for more
details on the use of CC Mode hooks.
(setq c-default-style '((java-mode . "java")
(awk-mode . "awk")
(other . "free-group-style")))
See Styles for fuller
details on using CC Mode styles and how to create
them.
(defun my-c-mode-hook ()
(c-set-style
(if (and (buffer-file-name)
(string-match "/usr/src/linux" (buffer-file-name)))
"linux"
"free-group-style")))
(add-hook 'c-mode-hook 'my-c-mode-hook)
In a programming team, a hook is a also a good place for each member to put his own personal preferences. For example, you might be the only person in your team who likes Auto-newline minor mode. You could have it enabled by default by placing the following in your .emacs:
(defun my-turn-on-auto-newline ()
(c-toggle-auto-newline 1))
(add-hook 'c-mode-common-hook 'my-turn-on-auto-newline)